

package com.tjsiqi.service;

import com.tjsiqi.beans.PageQuery;
import com.tjsiqi.beans.PageResult;
import com.tjsiqi.dao.StaffCheckInMapper;
import com.tjsiqi.dto.StaffCheckInDto;
import com.tjsiqi.exception.ParamException;
import com.tjsiqi.model.StaffCheckIn;
import com.tjsiqi.model.StaffCheckInEXP;
import com.tjsiqi.param.StaffCheckInParam;
import com.tjsiqi.util.DateTimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class SysStaffCheckInService {
    @Autowired
    private StaffCheckInMapper staffCheckInMapper;

    //  查询考勤记录
    public PageResult<StaffCheckIn> searchPageList(StaffCheckInParam staffCheckInParam, PageQuery page) {

        StaffCheckInDto staffCheckInDto = getStaffCheckInDto(staffCheckInParam);

        int count = staffCheckInMapper.getStaffCheckInCount(staffCheckInDto);

        System.out.println("WeighJblWeighTruckOUTVoCount"+count);
        if (count > 0){
            List<StaffCheckIn> staffCheckInList = staffCheckInMapper.getPageList(staffCheckInDto, page);
            return PageResult.<StaffCheckIn>builder().total(count).data(staffCheckInList).build();
        }
        return PageResult.<StaffCheckIn>builder().build();
    }

//  封装查询数据
    private static StaffCheckInDto getStaffCheckInDto(StaffCheckInParam staffCheckInParam){
        StaffCheckInDto staffCheckInDto = new StaffCheckInDto();

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (StringUtils.isNotBlank(staffCheckInParam.getFromTime())) {
                staffCheckInDto.setFromTime(dateFormat.parse(staffCheckInParam.getFromTime()+" 00:00:00"));
            }else{
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.HOUR_OF_DAY, 0);
                // 分
                cal.set(Calendar.MINUTE, 0);
                // 秒
                cal.set(Calendar.SECOND, 0);
                // 毫秒
                cal.set(Calendar.MILLISECOND, 0);
                cal.add(Calendar.DATE, 0);
                cal.add(Calendar.MONTH,-3);
                staffCheckInDto.setFromTime(cal.getTime());
//                System.out.println("测试时间"+ DateTimeUtil.dateToStr(staffCheckInDto.getFromTime()));
            }
            if (StringUtils.isNotBlank(staffCheckInParam.getToTime())) {
                staffCheckInDto.setToTime(dateFormat.parse(staffCheckInParam.getToTime()+" 23:59:59"));
            }else{
                Calendar calTo = Calendar.getInstance();
                calTo.set(Calendar.HOUR_OF_DAY, 23);
                // 分
                calTo.set(Calendar.MINUTE, 59);
                // 秒
                calTo.set(Calendar.SECOND, 59);
                // 毫秒
                calTo.set(Calendar.MILLISECOND, 0);
                staffCheckInDto.setToTime(calTo.getTime());
            }
        } catch (Exception e) {
            throw new ParamException("传入的日期格式有问题，正确格式为：yyyy-MM-dd HH:mm:ss");
        }

        staffCheckInDto.setJbzCode(staffCheckInParam.getJbzCode());

        if (StringUtils.isNotBlank(staffCheckInParam.getFullname())){
            staffCheckInDto.setFullname(staffCheckInParam.getFullname());
        }else {
            staffCheckInDto.setFullname(null);
        }

        if (StringUtils.isNotBlank(staffCheckInParam.getUnitname())){
            staffCheckInDto.setUnitname(staffCheckInParam.getUnitname());
        }else {
            staffCheckInDto.setUnitname(null);
        }

        return staffCheckInDto;
    }

    //  导出考勤表
    public List<StaffCheckInEXP> exportByParam(StaffCheckInParam staffCheckInParam) {

        StaffCheckInDto staffCheckInDto = getStaffCheckInDto(staffCheckInParam);

        List<StaffCheckInEXP> staffCheckInEXPList = staffCheckInMapper.exportByParam(staffCheckInDto);

        return staffCheckInEXPList;
    }
}
